<?php
	class User extends AppModel {
		public $validate = array(
				'name' =>array(
// 					'Please enter your name.'=>array(
// 						'rule'=>'',
// 						'message'=>'Please enter your name'
// 					)
				),
				'username' => array(
					'required.' => array(
						'rule' => array('notEmpty'),
						'message' => 'A username is required'
					),
					'The username must be between 2 and 15 characters.'=>array(
						'rule'=>array('between', 2, 15),
						'message'=>'The username must be between 5 and 15 characters.'
					),
					'That username has already been taken'=>array(
						'rule'=>'isUnique',
						'message'=>'That username has already been taken.'
					)
				),
				'email'=>array(
					'Valid email'=>array(
						'rule'=>array('email'),
						'message'=>'Please enter a valid email address'
					)
				),
				'password' => array(
					'required' => array(
						'rule' => array('notEmpty'),
						'message' => 'A password is required'
					),
					'Match passwords'=>array(
							'rule'=>'matchPasswords',
							'message'=>'Your passwords do not match'
					)
				),
				'password_confirmation'=>array(
						'Not empty'=>array(
								'rule'=>'notEmpty',
								'message'=>'Please confirm your password'
						)
				),
				'role' => array(
						'valid' => array(
								'rule' => array('inList', array('admin', 'author')),
								'message' => 'Please enter a valid role',
								'allowEmpty' => false
						)
				)
		);

		public function matchPasswords($data) {
			if ($data['password'] == $this->data['User']['password_confirmation']) {
				return true;
			}
			$this->invalidate('password_confirmation', 'Your passwords do not match');
			return false;
		}
		public function beforeSave($options = array()) {
			if (isset($this->data[$this->alias]['password'])) {
				$this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
			}
			return true;
		}
	}